﻿using BCData.Sim.Company.Company;
using BCData.Sim.CompanyBelongArea;
using BCDto.Sim.Admin.Admin;
using BCDto.Sim.CompanyBelongArea;
using BCEntity.Sim.Company.Company;
using BCEntity.Sim.CompanyBelongArea;
using BCService.Utils;
using System;
using System.Collections.Generic;
using System.Linq;

namespace BCService.Sim.CompanyBelongArea
{
    public class CompanyBelongAreaService : ICompanyBelongAreaService
    {
        private readonly ICompanyData companyData;
        private readonly ICompanyBelongAreaData companyBelongAreaData;

        public CompanyBelongAreaService(ICompanyData companyData,
            ICompanyBelongAreaData companyBelongAreaData)
        {
            this.companyData = companyData;
            this.companyBelongAreaData = companyBelongAreaData;
        }

        public IEnumerable<CompanyBelongAreaDto> OtherBelongArea(int companyId)
        {
            return this.companyBelongAreaData.OtherBelongArea(companyId).GetAwaiter().GetResult().As<IEnumerable<CompanyBelongAreaDto>>();
        }

        public IEnumerable<CompanyBelongAreaDto> Query(int companyId)
        {
            return this.companyBelongAreaData.Query(companyId).GetAwaiter().GetResult().As<IEnumerable<CompanyBelongAreaDto>>();
        }

        public IEnumerable<CompanyBelongAreaDto> SettingBelongArea(AdminDto adminDto, int companyId, List<CompanyBelongAreaRequestDto> companyBelongAreaRequestDtos)
        {
            IEnumerable<CompanyBelongAreaEntity> companyBelongAreaEntities = this.companyBelongAreaData.Query(companyId).GetAwaiter().GetResult();
            CompanyEntity companyEntity = this.companyData.GetEntity(companyId).GetAwaiter().GetResult();
            List<CompanyBelongAreaEntity> insertList = new List<CompanyBelongAreaEntity>();
            List<CompanyBelongAreaEntity> deleteList = new List<CompanyBelongAreaEntity>();
            //if (companyEntity.CompanyStatus == GroupWorkStatus.Cancelled)
            //{
            //    throw new ArgumentException("公司已注销，无法继续设置归属");
            //}
            foreach (var item in companyBelongAreaRequestDtos.Where(p => !p.CompanyBelongAreaId.HasValue))
            {
                if (!companyBelongAreaEntities.Any(p => p.ProvinceId == item.ProvinceId && p.CityId == item.CityId))
                {
                    CompanyBelongAreaEntity entity = new CompanyBelongAreaEntity
                    {
                        CompanyId = companyId,
                        CompanySubName = companyEntity.CompanySubName,
                        CompanyName = companyEntity.CompanyName,
                        ProvinceId = item.ProvinceId,
                        CityId = item.CityId,
                        CreateAdminId = adminDto.AdminId,
                        CreateAdminName = adminDto.FullName
                    };
                    insertList.Add(entity);
                }
            }

            foreach (var item in companyBelongAreaEntities)
            {
                if (!companyBelongAreaRequestDtos.Any(p => p.ProvinceId == item.ProvinceId && p.CityId == item.CityId))
                {
                    deleteList.Add(item);
                }
            }

            bool result = this.companyBelongAreaData.SettingBelongArea(insertList, deleteList).GetAwaiter().GetResult() > 0;

            if (!result)
            {
                throw new ArgumentException("服务器跑偏了，请重试");
            }

            return this.companyBelongAreaData.Query(companyId).GetAwaiter().GetResult().As<IEnumerable<CompanyBelongAreaDto>>();
        }
    }
}
